package com.hbnu.controller;

import com.hbnu.pojo.Door;
import com.hbnu.pojo.Order;
import com.hbnu.service.DoorsService;
import com.hbnu.service.OrdersService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class OrdersController {
    @Resource
    private OrdersService ordersService;

    @Resource
    private DoorsService doorsService;

    @RequestMapping("/orderList")
    public String getOrders(Model model){
        List<Order> orderList = ordersService.getOrders();
        List<Door> doorList = doorsService.getDoors();
        model.addAttribute("orders",orderList);
        model.addAttribute("doors",doorList);
        return "order_list";
    }

    @RequestMapping("/orderInfo")
    public String getOrdersById(Model model,Integer id){
        Order order = ordersService.getOrderById(id);
        List<Door> doorList = doorsService.getDoors();
        model.addAttribute("order",order);
        model.addAttribute("doors",doorList);
        return "order_update";
    }

    @RequestMapping("/deleteOrder")
    public String deleteOrderById(Integer id){
        int flag = ordersService.deleteOrderById(id);
        return "redirect:/orderList";
    }

    @RequestMapping("orderAddPage")
    public String OrderAddPage(Model model){
        List<Door> doorList = doorsService.getDoors();
        model.addAttribute("doors",doorList);
        return "order_add";
    }
    @RequestMapping("/addOrder")
    public String addOrder(Order order,Model model){
        System.out.println(order.toString());
        int flag = ordersService.addOrder(order);
        return "redirect:/orderList";
    }

    @RequestMapping("/updateOrder")
    public String updateOrder(Order order,Model model){
        int flag = ordersService.updateOrder(order);
        return "redirect:/orderList";
    }

}
